package com.one.tools.report.util;

import com.aspose.cells.*;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperRunManager;

import java.io.*;
import java.math.BigDecimal;
import java.sql.Connection;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public class ReportGenUtils {

    public ReportGenUtils() {

    }

    public static void gen(InputStream sourFile, Map<String, Object> parameters, OutputStream sosRef, Connection connection) throws Exception {
        InputStream inputStream = null;
        parameters.put("REPORT_LOCALE", Locale.ENGLISH);

        try {
            ByteOutputStream outputStream = new ByteOutputStream();
            JasperCompileManager.compileReportToStream(sourFile, outputStream);
            inputStream = new ByteArrayInputStream(outputStream.getBytes());
            JasperRunManager.runReportToPdfStream(inputStream, sosRef, parameters, connection);
        } catch (Exception var9) {
            throw new JRException("report gen error", var9);
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }

    public static void cell2PDF(InputStream sourFile, OutputStream sosRef) throws Exception {
        Workbook wb = new Workbook(sourFile);
        wb.save(sosRef, SaveFormat.PDF);
        sourFile.close();
        sosRef.close();
    }

    public static void getLicense() throws Exception {
        InputStream is = ReportGenUtils.class.getClassLoader().getResourceAsStream("license.xml");
        License license = new License();
        license.setLicense(is);
    }

    private void addChart(Workbook wb, List<Map<String, Object>> data, OutputStream out){
        try {
            ReportGenUtils.getLicense();
            WorkbookDesigner designer = new WorkbookDesigner(wb);
            com.aspose.cells.Workbook workbook = designer.getWorkbook();
            Worksheet sheet = workbook.getWorksheets().get("Percentage");
            workbook.getWorksheets().add("D");
            Worksheet sheetData = workbook.getWorksheets().get("D");
            Cells cells = sheetData.getCells();
            //图表数据
            for(int i = 0,l = data.size();i<l;i++){
                cells.get("M" + (i + 1)).putValue(((LocalDateTime)data.get(i).get("date")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")));
                cells.get("N" + (i + 1)).putValue(BigDecimal.valueOf(((Double) data.get(i).get("percent"))*100).setScale(2,BigDecimal.ROUND_HALF_UP));
            }
            sheet.getCharts().add(37, 0, 0, 20, 9);
            Chart chart = sheet.getCharts().get(0);
            chart.getTitle().setText("Percentage");
            //chart.getCategoryAxis().getMajorGridLines().setVisible(false);
            //数据源
            int count = data.size();
            chart.getNSeries().add("D!N1:N"+count, true);
            //类别
            chart.getNSeries().setCategoryData("D!M1:M"+count);
            for (int i = 0; i < chart.getNSeries().getCount(); i++)
            {
                chart.getNSeries().get(i).getSeriesLines().setWeight(WeightType.MEDIUM_LINE);
                chart.getNSeries().get(i).getMarker().setMarkerSize(5);
                chart.getNSeries().get(i).getMarker().setMarkerStyle(ChartMarkerType.CIRCLE);
                chart.getNSeries().get(i).getMarker().getBorder().setColor(Color.getDarkGreen());
            }
            chart.setShowLegend(false);
            chart.getValueAxis().getTitle().setText("%");
            chart.getValueAxis().setMaxValue(150);
            chart.getValueAxis().setMinValue(0);
            chart.getPlotArea().getArea().setForegroundColor(Color.getWhite());
            designer.process();
            sheetData.setVisible(false);
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            pdfSaveOptions.setPrintingPageType(PrintingPageType.IGNORE_BLANK);
            //设置分页信息
            this.sheetsPage(workbook);
            workbook.save(out,pdfSaveOptions);
        }catch (Exception e){
            System.out.println("cell转pdf异常" + e);
        }
    }
    /**
     * 设置分页心爱
     * @param wb
     * */
    private void sheetsPage(com.aspose.cells.Workbook wb){
        for (int i = 0,l = wb.getWorksheets().getCount();i < l;i++){
            wb.getWorksheets().get(i).getPageSetup().setFooter(2, "&P of &N");
        }
    }
}
